package com.travelranking.view;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.model.SelectItem;

import com.travelranking.pojo.City;
import com.travelranking.pojo.Travel;
import com.travelranking.service.CityService;
import com.travelranking.service.RankingService;

public class RankingAction {
	private Travel travel;
	private RankingService rankingService;
	private CityService cityService;
	

	public RankingAction() {
		travel = new Travel();
	}

	public String add() {
		String navigation = "NEW";
		if (validadteFields()){
			rankingService.add(travel);
			
			addFacesMessage("Nova viagem salva com sucesso.");
			navigation="LIST";
		}
		return navigation;
	}

	private boolean validadteFields(){
		boolean result = true;
		
		if (travel.getOpinion().isEmpty()){
			addFacesMessage("Informar impressões da viagem.");
			result = false;
		}
		if ((travel.getCity() == null) 
				|| (travel.getCity().getId() == null) 
				|| (travel.getCity().getId() == 0) 
				|| (travel.getCity().getCountry() == null )
				|| (travel.getCity().getCountry().getId() == null)
				|| (travel.getCity().getCountry().getId() == 0)){
			addFacesMessage("Informar lugar da viagem.");
			result = false;
		}
		
		if (travel.getRank() == null) {
			addFacesMessage("Informar nota da viagem.");
			result = false;
		}
		
		return result;
	}
	
	public List<SelectItem> getListRankValues() {
		List<SelectItem> s = new ArrayList<SelectItem>();
		//for (Integer v : rankingService.listRankValues()) {
		
		for (Integer v = 0; v <= 10; v++){
			s.add(new SelectItem(v, v.toString()));
		}
		
		return s;
	}
	
	public List<SelectItem> getListCityByCountry(){
		List<City> returnedData = null;
		City city = travel.getCity();
		List<SelectItem> result = new ArrayList<SelectItem>();
		
		if ((city.getCountry().getId() == null) || (city.getCountry().getId() == 0)) {
			returnedData = cityService.list();
		}else {
			returnedData = cityService.listByCountry(city.getCountry());
		}
		
		for (City returnedCity : returnedData){
			result.add(new SelectItem(returnedCity.getId(),returnedCity.getName()));
		}
		
		return result;
	}
	
	public void processCityByCountry(ActionEvent e){
		System.out.println("Processando: [processCityByCountry] country: "+travel.getCity().getCountry());
		getListCityByCountry();
	}

	public String cleanAndList() {
		travel = new Travel();
		return "LIST";
	}

	public String cleanFields() {
		travel = new Travel();
		return "NEW";
	}

	public void setListTravels(List<Travel> l) {
	}

	public List<Travel> getListTravels() {
		List<Travel> l = rankingService.list();
		return l;
	}

	public Travel getTravel() {
		return travel;
	}

	public void setTravel(Travel t) {
		this.travel = t;
	}

	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}
	public RankingService getRankingService() {
		return rankingService;
	}
	public void setRankingService(RankingService service) {
		this.rankingService = service;
	}
	public static void addFacesMessage(String s){
		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,s,s);
		FacesContext cont = FacesContext.getCurrentInstance();
		cont.addMessage(null, msg);
	}
}
